REM PAR_di=1
REM PAR_D=3
REM PAR_L=2
REM PAR_ci=0.2 'inner chamfer
REM PAR_co=0.2 'outer chamfer
REM PAR_aci=15
REM PAR_aco=45
REM 'type 21
REM PAR_r=0.3
REM PAR_b=1
REM PAR_D1=5
REM 'type 21
REM PAR_u=0.1 'type 22
REM PAR_Ds=3 'type 3
REM PAR_AXIS=1
REM VERSION=11 '1,11,21,22,3
REM PAR_R11=10
REM PAR_s4=0.6
REM PAR_d0=5

di=PAR_di
D=PAR_D
L=PAR_L
ci=PAR_ci
co=PAR_co
aci=PAR_aci/180*pi
aco=PAR_aco/180*pi
r=PAR_r
b=PAR_b
D1=PAR_D1
u=PAR_u
ud=0.2 'groove depth
Ds=PAR_Ds
If VERSION=3 Then yDs=sqr((Ds/2)^2-(L/2)^2)
REM ax=0.2
If L<10 Then ax=1 Else If L<50 Then ax=2 Else If L<100 Then ax=3 Else ax=5
REM hatchStep=1
If L<2 Then hatchStep=0.25 Else If L<5 Then hatchStep=0.5 Else If L<20 Then hatchStep=1 Else If L<50 Then hatchStep=2 Else If L<100 Then hatchStep=3 Else hatchStep=5
hatchAngle=45
LINESREST=0 '1 - to draw the rest of lines when PAR_AXIS=0
'groove with hole
R11=PAR_R11
s3=(D-di)/2
s4=PAR_s4
d0=PAR_d0
Rgr=(D/2+R11-(s3-s4))

const pi=3.141592653589793

REM If (D-di)/2-(ci*tan(aci)+co*tan(aco))<0.0001 Then co=((D-di)/2-ci*tan(aci))/tan(aco)*0.9 'checking to prevent sharp edge

Const POINT_ARR = 2
Const ko_MathPointParam = 14
Dim math, iDA, iPoint

Set math   = Kompas.GetMathematic2D
Set iDA    = Kompas.GetDynamicArray(POINT_ARR)
Set iPoint = Kompas.GetParamStruct(ko_MathPointParam)

Dim xGL,yGL,xGR,yGR,xHL,yHL,xHR,yHR

Sub CharacterPoints
'groove ark
GetIntersectArcLin L/2,Rgr,R11,180,270,1,co,D/2,0,xGL,yGL
REM Doc.ksCircle L/2,Rgr,R11,6
REM Doc.ksPoint xGL,yGL,0
xGR=L/2+(L/2-xGL)
yGR=yGL
'hole
GetIntersectArcLin L/2,Rgr,R11,180,270,1,L/2-d0/2,0,90,xHL,yHL
REM Doc.ksPoint xHL,yHL,3
xHR=L-xHL
yHR=yHL
REM Doc.ksPoint xHR,yHR,4
End Sub

Sub HalfView(k)
Doc.ksLineSeg L,(di/2+ci*tan(aci))*k,L-ci,di/2*k,1
Doc.ksLineSeg L-ci,di/2*k,ci,di/2*k,1
Doc.ksLineSeg ci,di/2*k,0,(di/2+ci*tan(aci))*k,1
If Not(VERSION=3) Then
	Doc.ksLineSeg 0,(di/2+ci*tan(aci))*k,0,(D/2-co*tan(aco))*k,1
	Doc.ksLineSeg 0,(D/2-co*tan(aco))*k,co,D/2*k,1
End If

If VERSION=1 Then
	Doc.ksLineSeg co,D/2*k,L-co,D/2*k,1
	Doc.ksLineSeg L-co,D/2*k,L,(D/2-co*tan(aco))*k,1
	Doc.ksLineSeg L,(D/2-co*tan(aco))*k,L,(di/2+ci*tan(aci))*k,1
ElseIf VERSION=11 Then
	If k=1 Then
			Doc.ksLineSeg co,D/2*k,xGL,D/2*k,1
			Doc.ksLineSeg xGR,D/2*k,L-co,D/2*k,1
			Doc.ksArcByPoint L/2,Rgr*k,R11,xGL,D/2*k,xGR,(D/2+r)*k,k,1
			
			Doc.ksLineSeg L/2,(di/2-ax)*k,L/2,(D/2+ax)*k,3
			
			Doc.ksLineSeg L-co,D/2*k,L,(D/2-co*tan(aco))*k,1
			Doc.ksLineSeg L,(D/2-co*tan(aco))*k,L,(di/2+ci*tan(aci))*k,1
		If xHL>xGL Then
			REM Doc.ksLineSeg co,D/2*k,xGL,D/2*k,1
			REM Doc.ksLineSeg xGR,D/2*k,L-co,D/2*k,1
			REM Doc.ksArcByPoint L/2,Rgr*k,R11,xGL,D/2*k,xGR,(D/2+r)*k,k,1
			
			Doc.ksLineSeg xHL,di/2*k,xHL,yHL*k,1
			Doc.ksLineSeg xHR,di/2*k,xHR,yHR*k,1
			REM Doc.ksLineSeg L/2,(di/2-ax)*k,L/2,(D/2+ax)*k,3
			
			REM Doc.ksLineSeg L-co,D/2*k,L,(D/2-co*tan(aco))*k,1
			REM Doc.ksLineSeg L,(D/2-co*tan(aco))*k,L,(di/2+ci*tan(aci))*k,1
		Else
			REM Doc.ksLineSeg co,D/2*k,xGL,D/2*k,1
			REM Doc.ksLineSeg xGR,D/2*k,L-co,D/2*k,1
			REM Doc.ksArcByPoint L/2,Rgr*k,R11,xGL,D/2*k,xGR,(D/2+r)*k,k,1
			
			Doc.ksLineSeg xHL,di/2*k,xHL,D/2*k,1
			Doc.ksLineSeg xHR,di/2*k,xHR,D/2*k,1
			REM Doc.ksLineSeg L/2,(di/2-ax)*k,L/2,(D/2+ax)*k,3
			
			REM Doc.ksLineSeg L-co,D/2*k,L,(D/2-co*tan(aco))*k,1
			REM Doc.ksLineSeg L,(D/2-co*tan(aco))*k,L,(di/2+ci*tan(aci))*k,1
		End If
	Else
	REM Doc.ksLineSeg co,D/2*k,L-co,D/2*k,1
	Doc.ksLineSeg co,D/2*k,xGL,D/2*k,1
	Doc.ksLineSeg xGR,D/2*k,L-co,D/2*k,1
	Doc.ksArcByPoint L/2,Rgr*k,R11,xGL,D/2*k,xGR,(D/2+r)*k,k,1
	
	Doc.ksLineSeg L-co,D/2*k,L,(D/2-co*tan(aco))*k,1
	Doc.ksLineSeg L,(D/2-co*tan(aco))*k,L,(di/2+ci*tan(aci))*k,1
	End If
ElseIf VERSION=21 Then
	Doc.ksLineSeg co,D/2*k,L-b-r,D/2*k,1
	Doc.ksArcByPoint L-b-r,(D/2+r)*k,r,L-b-r,D/2*k,L-b,(D/2+r)*k,k,1
	Doc.ksLineSeg L-b,(D/2+r)*k,L-b,(D1/2)*k,1
	Doc.ksLineSeg L-b,(D1/2)*k,L,(D1/2)*k,1
	Doc.ksLineSeg L,(D1/2)*k,L,(di/2+ci*tan(aci))*k,1
ElseIf VERSION=22 Then
	Doc.ksLineSeg co,D/2*k,L-b-u,D/2*k,1
	Doc.ksLineSeg L-b-u,D/2*k,L-b-u,(D/2-ud)*k,1
	Doc.ksLineSeg L-b-u,(D/2-ud)*k,L-b,(D/2-ud)*k,1
	Doc.ksLineSeg L-b,(D/2-ud)*k,L-b,(D1/2)*k,1
	Doc.ksLineSeg L-b,(D1/2)*k,L,(D1/2)*k,1
	Doc.ksLineSeg L,(D1/2)*k,L,(di/2+ci*tan(aci))*k,1
Else 'version 3
	Doc.ksLineSeg 0,(di/2+ci*tan(aci))*k,0,yDs*k,1
	Doc.ksArcByPoint L/2,0,Ds/2,0,yDs*k,L,yDs*k,-k,1
	Doc.ksLineSeg L,(di/2+ci*tan(aci))*k,L,yDs*k,1
End If

If PAR_AXIS Then
	Doc.ksLineSeg 0,0,0,(di/2+ci*tan(aci))*k,1
	Doc.ksLineSeg ci,0,ci,(di/2)*k,1
	Doc.ksLineSeg L,0,L,(di/2+ci*tan(aci))*k,1
	Doc.ksLineSeg L-ci,0,L-ci,(di/2)*k,1
Else
	If LINESREST Then
		Doc.ksLineSeg 0,di/2*k,0,(di/2+ci*tan(aci))*k,1
		Doc.ksLineSeg L,di/2*k,L,(di/2+ci*tan(aci))*k,1
	End If
End If
End Sub

Sub HalfHatch(k)
Doc.ksLineSeg L,(di/2+ci*tan(aci))*k,L-ci,di/2*k,1
Doc.ksLineSeg L-ci,di/2*k,ci,di/2*k,1
Doc.ksLineSeg ci,di/2*k,0,(di/2+ci*tan(aci))*k,1
If Not(VERSION=3) Then
	Doc.ksLineSeg 0,(di/2+ci*tan(aci))*k,0,(D/2-co*tan(aco))*k,1
	Doc.ksLineSeg 0,(D/2-co*tan(aco))*k,co,D/2*k,1
End If

If VERSION=1 Then
	Doc.ksLineSeg co,D/2*k,L-co,D/2*k,1
	Doc.ksLineSeg L-co,D/2*k,L,(D/2-co*tan(aco))*k,1
	Doc.ksLineSeg L,(D/2-co*tan(aco))*k,L,(di/2+ci*tan(aci))*k,1
ElseIf VERSION=11 Then
	If k=1 Then
		If xHL>xGL Then
			Doc.ksLineSeg co,D/2*k,xGL,D/2*k,1
			Doc.ksLineSeg xGR,D/2*k,L-co,D/2*k,1
			Doc.ksArcByPoint L/2,Rgr*k,R11,xGL,D/2*k,xHL,yHL*k,k,1
			Doc.ksArcByPoint L/2,Rgr*k,R11,xGR,D/2*k,xHR,yHR*k,-k,1
			
			Doc.ksLineSeg xHL,di/2*k,xHL,yHL*k,1
			Doc.ksLineSeg xHR,di/2*k,xHR,yHR*k,1
			
			Doc.ksLineSeg L-co,D/2*k,L,(D/2-co*tan(aco))*k,1
			Doc.ksLineSeg L,(D/2-co*tan(aco))*k,L,(di/2+ci*tan(aci))*k,1
		Else
			Doc.ksLineSeg co,D/2*k,xHL,D/2*k,1
			Doc.ksLineSeg xHR,D/2*k,L-co,D/2*k,1
			
			Doc.ksLineSeg xHL,di/2*k,xHL,D/2*k,1
			Doc.ksLineSeg xHR,di/2*k,xHR,D/2*k,1
			
			Doc.ksLineSeg L-co,D/2*k,L,(D/2-co*tan(aco))*k,1
			Doc.ksLineSeg L,(D/2-co*tan(aco))*k,L,(di/2+ci*tan(aci))*k,1
		End If
	Else
	REM Doc.ksLineSeg co,D/2*k,L-co,D/2*k,1
	Doc.ksLineSeg co,D/2*k,xGL,D/2*k,1
	Doc.ksLineSeg xGR,D/2*k,L-co,D/2*k,1
	Doc.ksArcByPoint L/2,Rgr*k,R11,xGL,D/2*k,xGR,(D/2+r)*k,k,1

	Doc.ksLineSeg L-co,D/2*k,L,(D/2-co*tan(aco))*k,1
	Doc.ksLineSeg L,(D/2-co*tan(aco))*k,L,(di/2+ci*tan(aci))*k,1
	End If
ElseIf VERSION=21 Then
	Doc.ksLineSeg co,D/2*k,L-b-r,D/2*k,1
	Doc.ksArcByPoint L-b-r,(D/2+r)*k,r,L-b-r,D/2*k,L-b,(D/2+r)*k,k,1
	Doc.ksLineSeg L-b,(D/2+r)*k,L-b,(D1/2)*k,1
	Doc.ksLineSeg L-b,(D1/2)*k,L,(D1/2)*k,1
	Doc.ksLineSeg L,(D1/2)*k,L,(di/2+ci*tan(aci))*k,1
ElseIf VERSION=22 Then
	Doc.ksLineSeg co,D/2*k,L-b-u,D/2*k,1
	Doc.ksLineSeg L-b-u,D/2*k,L-b-u,(D/2-ud)*k,1
	Doc.ksLineSeg L-b-u,(D/2-ud)*k,L-b,(D/2-ud)*k,1
	Doc.ksLineSeg L-b,(D/2-ud)*k,L-b,(D1/2)*k,1
	Doc.ksLineSeg L-b,(D1/2)*k,L,(D1/2)*k,1
	Doc.ksLineSeg L,(D1/2)*k,L,(di/2+ci*tan(aci))*k,1
Else 'version 3
	Doc.ksLineSeg 0,(di/2+ci*tan(aci))*k,0,yDs*k,1
	Doc.ksArcByPoint L/2,0,Ds/2,0,yDs*k,L,yDs*k,-k,1
	Doc.ksLineSeg L,(di/2+ci*tan(aci))*k,L,yDs*k,1
End If
End Sub

Sub HalfFilling(k)
REM Doc.ksLineSeg L,(di/2+ci*tan(aci))*k,L-ci,di/2*k,1
REM Doc.ksLineSeg L-ci,di/2*k,ci,di/2*k,1
REM Doc.ksLineSeg ci,di/2*k,0,(di/2+ci*tan(aci))*k,1
If Not(VERSION=3) Then
	Doc.ksLineSeg 0,(di/2+ci*tan(aci))*k,0,(D/2-co*tan(aco))*k,1
	Doc.ksLineSeg 0,(D/2-co*tan(aco))*k,co,D/2*k,1
End If

If VERSION=1 Then
	Doc.ksLineSeg co,D/2*k,L-co,D/2*k,1
	Doc.ksLineSeg L-co,D/2*k,L,(D/2-co*tan(aco))*k,1
	Doc.ksLineSeg L,(D/2-co*tan(aco))*k,L,(di/2+ci*tan(aci))*k,1
ElseIf VERSION=11 Then
	If k=1 Then
	Doc.ksLineSeg co,D/2*k,xGL,D/2*k,1
	Doc.ksLineSeg xGR,D/2*k,L-co,D/2*k,1
	Doc.ksArcByPoint L/2,Rgr*k,R11,xGL,D/2*k,xGR,(D/2+r)*k,k,1
	
	Doc.ksLineSeg L-co,D/2*k,L,(D/2-co*tan(aco))*k,1
	Doc.ksLineSeg L,(D/2-co*tan(aco))*k,L,(di/2+ci*tan(aci))*k,1
	Else
	REM Doc.ksLineSeg co,D/2*k,L-co,D/2*k,1
	Doc.ksLineSeg co,D/2*k,xGL,D/2*k,1
	Doc.ksLineSeg xGR,D/2*k,L-co,D/2*k,1
	Doc.ksArcByPoint L/2,Rgr*k,R11,xGL,D/2*k,xGR,(D/2+r)*k,k,1

	Doc.ksLineSeg L-co,D/2*k,L,(D/2-co*tan(aco))*k,1
	Doc.ksLineSeg L,(D/2-co*tan(aco))*k,L,(di/2+ci*tan(aci))*k,1
	End If
ElseIf VERSION=21 Then
	Doc.ksLineSeg co,D/2*k,L-b-r,D/2*k,1
	Doc.ksArcByPoint L-b-r,(D/2+r)*k,r,L-b-r,D/2*k,L-b,(D/2+r)*k,k,1
	Doc.ksLineSeg L-b,(D/2+r)*k,L-b,(D1/2)*k,1
	Doc.ksLineSeg L-b,(D1/2)*k,L,(D1/2)*k,1
	Doc.ksLineSeg L,(D1/2)*k,L,(di/2+ci*tan(aci))*k,1
ElseIf VERSION=22 Then
	Doc.ksLineSeg co,D/2*k,L-b-u,D/2*k,1
	Doc.ksLineSeg L-b-u,D/2*k,L-b-u,(D/2-ud)*k,1
	Doc.ksLineSeg L-b-u,(D/2-ud)*k,L-b,(D/2-ud)*k,1
	Doc.ksLineSeg L-b,(D/2-ud)*k,L-b,(D1/2)*k,1
	Doc.ksLineSeg L-b,(D1/2)*k,L,(D1/2)*k,1
	Doc.ksLineSeg L,(D1/2)*k,L,(di/2+ci*tan(aci))*k,1
Else 'version 3
	Doc.ksLineSeg 0,(di/2+ci*tan(aci))*k,0,yDs*k,1
	Doc.ksArcByPoint L/2,0,Ds/2,0,yDs*k,L,yDs*k,-k,1
	Doc.ksLineSeg L,(di/2+ci*tan(aci))*k,L,yDs*k,1
End If

If PAR_AXIS Then
	Doc.ksLineSeg 0,0,0,(di/2+ci*tan(aci))*k,1
	REM Doc.ksLineSeg ci,0,ci,(di/2)*k,1
	Doc.ksLineSeg L,0,L,(di/2+ci*tan(aci))*k,1
	REM Doc.ksLineSeg L-ci,0,L-ci,(di/2)*k,1
Else
	If LINESREST Then
		Doc.ksLineSeg L,di/2*k,0,di/2*k,1
		Doc.ksLineSeg 0,di/2*k,0,(di/2+ci*tan(aci))*k,1
		Doc.ksLineSeg L,di/2*k,L,(di/2+ci*tan(aci))*k,1
	Else
		Doc.ksLineSeg L,(di/2+ci*tan(aci))*k,L-ci,di/2*k,1
		Doc.ksLineSeg L-ci,di/2*k,ci,di/2*k,1
		Doc.ksLineSeg ci,di/2*k,0,(di/2+ci*tan(aci))*k,1
	End If
End If
End Sub

Sub Script_OnLoad()
If VERSION=11 Then CharacterPoints

HalfView 1
HalfView -1

REM Doc.ksMtr L*1.1,0,0,1,1
Doc.ksHatch 0, hatchAngle, hatchStep, 0, 0, 0 
	HalfHatch 1
	HalfHatch -1
Doc.ksEndObj
REM Doc.ksDeleteMtr

REM Doc.ksMtr L*1.1,0,0,1,1
Doc.ksColouring &HFE000000
	HalfFilling 1
	HalfFilling -1
Doc.ksEndObj
REM Doc.ksDeleteMtr

If PAR_AXIS Then
	Doc.ksLineSeg 0-ax,0,L+ax,0,3
End If
End Sub

'crossing of arc with line
Sub GetIntersectArcLin(ArcX,ArcY,ArcR,ArcAng1,ArcAng2,n,LineX,LineY,Angle,PointX1,PointY1)
   PointX1 = 0
   PointY1 = 0
   Math.ksIntersectArcLin ArcX,ArcY,ArcR,ArcAng1,ArcAng2,n,LineX,LineY,Angle,iDA
   If iDA.ksGetArrayCount > 0 then
    iDA.ksGetArrayItem 0,iPoint
      PointX1 = iPoint.x
      PointY1 = iPoint.y
	  REM kompas.ksmessage ("iPoint.x="&iPoint.x&" iPoint.y="&iPoint.y)
    iDa.ksClearArray
   End If
End Sub
